<?php

class Subject extends CI_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('subject_m');
        $this->load->model('chapter_m');
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
    }
    
    function getlist(){
        $arr=array();
        $subjects=$this->subject_m->select($arr);
        header("Content-Type:text/html;charset=utf8");
        //var_dump($subjects);
        if($subjects){
            //echo json_encode($subjects[0]);
//            foreach ($subjects as $key=>$val){
//                foreach($val as $k=>$v){
//                    //$s->$key=  urlencode($val);
//                    echo "k=$k,v=$v";
//                    $subjects[$key]->$k=  urlencode($v);
//                    var_dump($subjects[$key]);
//                }
//            }
            foreach($subjects as $s){
                foreach($s as $key=>$val){
                    $s->$key=  urlencode($val);
                }
                //var_dump($s);
            }
            //echo urldecode(json_encode($subjects));
            $result=array('code'=>'success','msg'=>'','data'=>  json_encode($subjects));
            
            echo urldecode(json_encode($result));
        }
    }
    
    function getdetail($sid){
        $list=$this->subject_m->getdetail($sid);
        if($list){
			header("Content-Type:text/html;charset=utf8");
            foreach ($list as $s){
                foreach($s as $key=>$val){
                    $s->$key=  urlencode($val);
                }
            }
            $result=array('code'=>'success','msg'=>'','data'=>json_encode($list));
            echo urldecode(json_encode($result));
        }
    }

    function add(){
        $hidden='';
        $attr='';
        $data=array('attr'=>$attr,'hidden'=>$hidden);
        $this->form_validation->set_rules('name','Name','required');
        $this->form_validation->set_rules('desc','Desc','required');
        //$this->form_validation->set_rules('imgfile','Imgfile','required');
        $err=array('nameerr'=>'','descerr'=>'','imgerr'=>'');
        
        if($this->form_validation->run()){
            $subject=$this->subject_m->select(array('name'=>$this->input->post('name')));
            if($subject){
                $err['nameerr']='课程已经存在';
                $this->load->view('subject/new',$err);
                return;
            }
                    $config['upload_path']='./upload/';
        $config['allowed_types']='gif|jpg|png';
        $config['max_size']=20000;
        $this->load->library('upload',$config);
        
            if($this->upload->do_upload('imgfile')){
                $data=array(
                    'name'=>$this->input->post('name'),
                    'desc'=>$this->input->post('desc'),
                    'imgUrl'=>$config['upload_path'].$this->upload->data()['file_name'],
                    'createTime'=>date('Y-m-d'),
                    'createBy'=>1,
                    'state'=>'');
                $this->subject_m->insert($data);
                $this->load->view('message',array('message'=>$data['name'].'课程添加完成','url'=>'subject/add'));
            }else{
                $error=array('upload_error'=>  $this->upload->display_errors());
                var_dump($error);
            }
        }else{
            $this->load->view('subject/new',$err);
        }
    }
}